package com.example.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.mapper.TokenMapper;
import com.example.mapper.UserMapper;
import com.example.model.Token;
import com.example.model.User;
import com.example.service.TokenService;
import com.example.service.UserService;
import com.example.util.StringUtil;

@Service
public class TokenServiceImpl implements TokenService {
	
	@Autowired
	TokenMapper tokenMapper;

	@Override
	public Token getTokenByKey(String key) {
		// TODO Auto-generated method stub
		return tokenMapper.getTokenByKey(key);
	}
	
	@Override
	public Token getTokenByValue(String value) {
		// TODO Auto-generated method stub
		return tokenMapper.getTokenByValue(value);
	}
	
	
	
	@Override
	public void insertToken(Token token) {
		Token tokenOld = tokenMapper.getTokenByKey(token.getTokenKey());
		if (!StringUtil.isBlank(tokenOld)) {
			tokenMapper.deleteToken(tokenOld);
			}
		tokenMapper.insertToken(token);
		
	}

	@Override
	public void deleteToken(Token token) {
		tokenMapper.deleteToken(token);
		
	}

	@Override
	public void updateToken(Token token) {
		tokenMapper.updateToken(token);
		
	}

	@Override
	public void logoutToken(Token token) {
		Token oldToken = tokenMapper.getTokenByValue(token.getTokenValue());
		if (!StringUtil.isBlank(oldToken)) {
			tokenMapper.deleteToken(oldToken);
		}
	}

}
